package com.ljx.itbbs.server.article.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.server.article.domain.entity.ResourceEntity;
import com.ljx.itbbs.server.article.mapper.ResourceMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceDao extends ServiceImpl<ResourceMapper, ResourceEntity> {

    public void batchUpdateResource(List<Long> resourcesList, Integer isFree, Integer integral, Long articleId) {
        this.baseMapper.batchUpdateResource(resourcesList, isFree, integral, articleId);
    }

    public List<ResourceEntity> getByArticleId(Long articleId) {
        return lambdaQuery().eq(ResourceEntity::getArticleId, articleId).list();
    }

    public void increaseDownloadNum(Long resourceId) {
        lambdaUpdate().setSql("download_num = download_num + 1")
                .eq(ResourceEntity::getId, resourceId)
                .update();
    }

    public void deleteArticleResource(Long articleId) {
        this.baseMapper.delete(new LambdaQueryWrapper<ResourceEntity>().eq(ResourceEntity::getArticleId, articleId));
    }

    public void clearArticleId(Long articleId) {
        lambdaUpdate().set(ResourceEntity::getArticleId, null)
                .set(ResourceEntity::getIntegral, null)
                .set(ResourceEntity::getIsFree, null)
                .eq(ResourceEntity::getArticleId, articleId)
                .update();
    }
}
